# -*- coding: utf-8 -*-

"""
@author: yangshuang
@software: PyCharm
@Project -> File：studyFastapi -> 2
@Date: 2021/11/17 16:50
"""

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {"被优先匹配到：": user_id}


@app.get("/users/me")
async def read_user_me():
    return {"user_id": "the current user"}


@app.get("/user/{user_id}/items/{item_id}")
async def read_user_item(user_id: int, item_id: str, q: str = None, short: bool = False):
    item = {"owner_id": user_id, "item_id": item_id}
    if q:
        item.update({"q":q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item


@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
    item = {"item_id": item_id, "needy": needy}
    return item
'''上述的代码中 needy 没有给与默认的值，当个没提交这个值的时候，会提示错误'''

from typing import Optional

@app.get("/items/{item_id}")
async def read_user_item(item_id: str, limit: Optional[int] = None):
    item = {"item_id": item_id, "limit": limit}
    return item


from enum import Enum

class ModelName(str, Enum):
    beijing = "1"
    tianjin = "2"

@app.get("/model/{model_name}")
async def get_model(model_name: ModelName):
    if model_name == ModelName.beijing:
        return {"model_name": model_name, "message": "北京"}
    if model_name.value == "2":
        return {"model_name": model_name, "message": "天津"}

if __name__ == '__main__':
    uvicorn.run(app='2:app', host="127.0.0.1", port=8000, reload=True, debug=True)
